//
//  HearwareViewController.m
//  PCNews
//
//  Created by PengchengWang on 16/3/4.
//  Copyright © 2016年 pengchengWang. All rights reserved.
//

#import "HearwareViewController.h"
#define kLastID 0

@interface HearwareViewController ()

@end

@implementation HearwareViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setBeginViews];
    [self loopImagesBound];
    [self dataBound];
    [self setViews];
    
    // Do any additional setup after loading the view.
}
-(void)setBeginViews{
    self.lastID = kLastID;
    self.upImagesUrl = kImageUrlHard;
}

///绑定tableView数据
-(void)dataBound{
    //创建请求第几页的数据URL
    NSString *urlStr = [NSString stringWithFormat:@"http://m.mydrivers.com/app/newslist.aspx?tid=1&minId=%ld&ver=2.1&temp=145679900043.677307",(long)self.lastID];
    [URLRequestManager requestUrlWithType:GET strURL:urlStr condition:nil success:^(id item) {
        //当请求到最后一条数据ID为"0"时,证明请求第一组数据,清空大数组
        if (self.lastID == kLastID) {
            self.arrAllData = nil;
        }
        for (NSDictionary *dic in item) {
            BaseModel *model = [[BaseModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.arrAllData addObject:model];
        }
        //取得当前获取到的数组中最后一条数据的ID,作为请求下一组数据的参数(接口文档要求)
        BaseModel *model = self.arrAllData[self.arrAllData.count-1];
        self.lastID = model.id;
        [self.tableV reloadData];
        [self endRefresh];
    } faile:^(NSError *error) {
        [self endRefresh];
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.mode = MBProgressHUDModeText;
        hud.labelText = @"您的网络不给力!";
        [hud hide: YES afterDelay: 2];
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
